import asyncio
import requests
import os

TEMP_PATH = os.path.join(os.path.abspath(os.path.join(os.getcwd(),"..")), 'temp')
async def download(url):
    print("开始下载,",url)
    #Wrapper Future
    loop = asyncio.get_event_loop()
    future = loop.run_in_executor(None,requests.get,url)
    rs = await future

    if rs.status_code == 200:
        filename =os.path.join(TEMP_PATH,url.split('/')[-1])
        with open(filename,'wb') as f:
            f.write(rs.content)
        print("下载结束,", url)
    else:
        print("下载失败")

def main():
    urls = [
        'https://17dz.com/home/images/auto-1.png',
        'https://17dz.com/home/images/auto-2.png',
        'https://17dz.com/home/images/auto-3.png'
    ]
    tasks = [download(url) for url in urls]
    loop = asyncio.get_event_loop()
    loop.run_until_complete(asyncio.wait(tasks))
    #await asyncio.wait(tasks)


if __name__ == "__main__":
    #asyncio.run(main())
    main()